//
//  ConExchangeViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/9/6.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConExchangeViewController.h"
#import "ExchangeCell.h"
#import "ConExchangeListController.h"
#import "ActivityNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "ActivityGoodsListModel.h"
#import "UIImage+Extension.h"
#import "ActivityGoodsDetailViewController.h"
#import <MJRefresh.h>

@interface ConExchangeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic, assign) int pageNo;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ConExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"兑换商品";
    _pageNo =  1;
    _dataArray = [NSMutableArray array];
    [self setUpNavi];
    [self.view addSubview:self.collection];
    [self loadData];
    
}
- (void)loadData {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNo"] = @(_pageNo);
    param[@"orderBy"] = @"0";
    [ActivityNetWork getActivityGoodsListWithParame:param response:^(BOOL success, NSString *msg, NSArray *moneyArr) {
       
        if (success) {
            [_collection.mj_header endRefreshing];
            [_collection.mj_footer endRefreshing];
            for (NSDictionary *dict in moneyArr) {
                ActivityGoodsListModel *model = [[ActivityGoodsListModel alloc] init];
                [model modelSetValueWithDict:dict];
                
                [_dataArray addObject:model];
            }
            [_collection reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
        
    }];
}
- (void)setUpNavi
{
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    [rightBtn setTitle:@"兑换清单" forState:UIControlStateNormal];
    [rightBtn setTitleColor:RGBColor(255, 255, 255) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = Font(16);
    [rightBtn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
- (void)rightClick
{
    ConExchangeListController *listVc = [[ConExchangeListController alloc]init];
    [self.navigationController pushViewController:listVc animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ExchangeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ExchangeCell class]) forIndexPath:indexPath];
    ActivityGoodsListModel *model = _dataArray[indexPath.row];
    [cell.goodsImage sd_setImageWithURL:[NSURL URLWithString:model.thumbnailUrl]];
    cell.goodsNameLabel.text = model.name;
    cell.activityValue.text =[NSString stringWithFormat:@"%@",model.activeValue];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ActivityGoodsDetailViewController *avc = [[ActivityGoodsDetailViewController alloc] init];
    
    ActivityGoodsListModel *model = _dataArray[indexPath.row];
    avc.productNo = model.productNo;
    NSLog(@"model.productnumber = %@",model.productNo);
    [self.navigationController pushViewController:avc animated:YES];
    
}
- (UICollectionView *)collection
{
    if (!_collection) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.itemSize = CGSizeMake((ScreenW-1)/2.0, (ScreenW-1)/2.0-20+70);
        flow.minimumLineSpacing = 1;
        flow.minimumInteritemSpacing = 1;
        _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) collectionViewLayout:flow];
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.backgroundColor = RGBColor(239, 239, 244);
        [_collection registerNib:[UINib nibWithNibName:NSStringFromClass([ExchangeCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ExchangeCell class])];
        _collection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _pageNo = 1;
            [_dataArray removeAllObjects];
            [self loadData];
        }];
        _collection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _pageNo++;
            [self loadData];
        }];
    }
    return _collection;
}

@end
